package com.example.demo.setstudy;

import java.util.Objects;

/**
 * @Description Student
 * @Author songchuanlong
 * @Date 2025-05-12
 */
public class Student implements Comparable<Student> {
    private String name;
    private int age;
    private double height;


    //重写equals和hashCode以保证哈希值一样
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Student student = (Student) o;
        return age == student.age && Double.compare(height, student.height) == 0 && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, height);
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }


    public Student(String name, int age, double height) {
        this.name = name;
        this.age = age;
        this.height = height;
    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", height=" + height +
                '}';
    }

    //this
    @Override
    public int compareTo(Student o) {
//        如果认为左边对象大于右边对象返回正整数
//        如果认为左边对象小于右边对象返回负整数
//        如果认为左边对象等于右边对象返回0
        return this.age-o.age;//情况一：比较年龄
    }
}
